package com.pxxy.springmvc.config;

import com.pxxy.springmvc.interceptor.PerformanceLogInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.*;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

/******************
 * @Author yejf
 * @Description TODO
 */
@Configuration   //表示此类是Spring框架的配置类
@ComponentScan({"com.pxxy.springmvc.controller","com.pxxy.springmvc.handlers"})
@EnableWebMvc //启动 Spring MVC
@Import(AppConfig.class)   //如有需要，则导入业务层和持久层
//@ImportResource("classpath:spring-mvc.xml")  //读取 xml 配置文件
public class WebMvcConfig implements WebMvcConfigurer {

    private static final Logger log = LoggerFactory.getLogger(WebMvcConfig.class);

    public WebMvcConfig() {
        log.debug("WebMvcConfig被创建了....");
    }

    /******
     * 重写此方法，用来告诉前置控制器 放行我们指定的静态资源
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.debug("调用了 addResourceHandlers 方法，放行 /static和/public 两个静态资源路径");
        //放行 static下的所有资源
        registry.addResourceHandler("/static/**")
                .addResourceLocations("/static/");
        registry.addResourceHandler("/public/**")
                .addResourceLocations("classpath:/public/");
    }

    /******
     * 配置一下视图解器
     * @param registry
     */
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        //
        log.debug("调用了 configureViewResolvers 方法，配置视图解析器");
        InternalResourceViewResolver irvr = new InternalResourceViewResolver();
        //设置属性
        irvr.setPrefix("/WEB-INF/jsp/");
        irvr.setSuffix(".jsp");
        irvr.setViewClass(JstlView.class); //默认也是 JstlView, 也就是 jsp
        irvr.setContentType("text/html;charset=utf-8");
        //
        registry.viewResolver(irvr);
    }

    /****
     * 做文件上传时所用的 bean
     * @return
     */
    @Bean
    public MultipartResolver multipartResolver() {
        //创建一个 CommonsMultipartResolver
        CommonsMultipartResolver multipartResolver =
                                new CommonsMultipartResolver();
        //配置属性
        multipartResolver.setMaxUploadSize(2*1024*1025); //最大2M
        multipartResolver.setMaxUploadSizePerFile(2*1024*1024); //每个文件大小
        //返回
        return multipartResolver;
    }

    /*****
     * 用来指定具体的Bean Validation
     * @return
     */
    @Override
    public Validator getValidator() {
        LocalValidatorFactoryBean lvfb = new LocalValidatorFactoryBean();
        //lvfb.setProviderClass(HibernateValidator.class);
        //设置属性
        lvfb.setValidationMessageSource(messageSource());
        //返回
        return lvfb;

    }

    /*****
     * 用来指定验证时要读取的资源文件
     * @return
     */
    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        //属性
        messageSource.setBasename("beanValidation");
        messageSource.setDefaultEncoding("UTF-8");
        messageSource.setCacheSeconds(60);
        messageSource.setAlwaysUseMessageFormat(true);
        //
        return messageSource;
    }

    /*****
     * 添加拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new PerformanceLogInterceptor()).addPathPatterns("/**");
    }


}
